package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Coupon;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.utils.CommonUtil;
import com.cskaoyan.utils.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Description
 * 优惠卷接口
 *
 * @author Zhangfei
 * @date Created on 2021/10/19
 */
@RestController
@RequestMapping("/admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    /**
     * 通过分页显示广告接口
     *
     * @param param
     * @return
     */
    @RequiresPermissions("admin:coupon:list")
    @GetMapping("/list")
    public BaseRespVo list(String name, Integer status, Integer type, BaseParam param) {
        BasePageData data = couponService.list(name, type, status, param);
        return BaseRespVo.ok(data);
    }

    /**
     * 通过id查询优惠卷
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("/read")
    public BaseRespVo read(Integer id) {
        Coupon coupon = couponService.read(id);
        return BaseRespVo.ok(coupon);
    }

    /**
     * 通过分页和优惠卷id查询使用了该优惠卷的用户
     *
     * @param couponId
     * @param param
     * @return
     */
    @RequiresPermissions("admin:coupon:listuser")
    @GetMapping("/listuser")
    public BaseRespVo listuser(Integer couponId, Integer userId, Integer status, BaseParam param) {
        BasePageData data = couponService.getUserByCouponId(userId, couponId, status, param);
        return BaseRespVo.ok(data);
    }


    /**
     * 添加优惠卷
     *
     * @param coupon
     * @return
     */
    @RequiresPermissions("admin:coupon:create")
    @PostMapping("/create")
    public BaseRespVo create(@RequestBody Coupon coupon) {
        Date startTime = coupon.getStartTime();
        Date endTime = coupon.getEndTime();
        Integer days = coupon.getDays();
        //判断参数
        if(startTime!=null&&endTime!=null&& DateUtil.compareDate(startTime, endTime)<0){
             return BaseRespVo.fail("添加格式不对");
        }

        Coupon couponToReturn = couponService.create(coupon);
        if (CommonUtil.isNull(couponToReturn)) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(couponToReturn);
    }

    /**
     * 删除优惠卷
     *
     * @param coupon
     * @return
     */
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Coupon coupon) {
        //执行删除，根据返回值响应相应的数据
        Integer count = couponService.delete(coupon);
        if (count != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }


    /**
     * 修改优惠卷
     *
     * @param coupon
     * @return
     */
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("/update")
    public BaseRespVo update(@RequestBody Coupon coupon) {
        Date startTime = coupon.getStartTime();
        Date endTime = coupon.getEndTime();
        Integer days = coupon.getDays();
        //判断参数
        if(startTime!=null&&endTime!=null&& DateUtil.compareDate(startTime, endTime)<0){
            return BaseRespVo.fail("添加格式不对");
        }
        //更新返回响应结果
        Coupon couponToReturn = couponService.update(coupon);
        return BaseRespVo.ok(couponToReturn);

    }


}
